package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.common.constans.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class DownOrUpListener {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    /**
     * 监听kafka的方法
     */
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void getMessage(String message){
        //1.转化类型
        Map map = JSON.parseObject(message, Map.class);
        Long articleId = (Long) map.get("articleId");
        Integer enable = (Integer) map.get("enable");
        //2.修改文章配置
            //根据文章id去查询aparticleconfig
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articleId));
        if(apArticleConfig!=null){
            /**
             * enable=0 表示下架，1表示上架
             * isdown=true 表示下架，false表示上架
             */
            apArticleConfig.setIsDown(enable==0?true:false);
            apArticleConfigMapper.updateById(apArticleConfig);
        }

    }
}
